#!/usr/bin/python
# -*- coding: utf-8 -*-

#Author: Yuanhao Li <jay_21cn@hotmail.com>
#File: tabmanager.py
#Usage: Client of ING from project Babel
#Copyright (C) 2007 Yuanhao Li<jay_21cn@hotmail.com>

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software Foundation,
#Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#本程序是遵从 GPL 2.0 许可发布的. 你可以自由修改和再发布本程序, 
#但请遵守 GPL 2.0 条款, 尊重知识产权.

import sys, os

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QString, QObject, QStringList

from ingcview import IngcView

cwd = os.path.split(sys.argv[0])[0]
if not cwd:
	cwd = os.getcwd()

class TabManager(QObject):
	def __init__(self, tabWidget):
		QObject.__init__(self)
		self.tabWidget = tabWidget
		self.views = []
		self.tempviews = []
		self.ingcApp = self.tabWidget.parent().parent().parent()

	def addView(self, url, usr, usr_pwd, proxy):
		contained = False
		tabText = usr + ' @' + url.split('http://', 1)[1]
		containedIndex = 0
		for i in range(self.tabWidget.count()):
			if QString(tabText) == self.tabWidget.tabText(i):
				contained = True
				containedIndex = i
		if contained is False:
			tab = QtGui.QWidget()
			tab.setObjectName(url)
			view = IngcView(url, usr, usr_pwd, proxy, self.ingcApp, tab)
			QtCore.QObject.connect(view, QtCore.SIGNAL("newItemsComing"), self.updateStar)
			QtCore.QObject.connect(view, QtCore.SIGNAL("requestedOnce2"), self.setStatusBarLastUpdate)
			QtCore.QObject.connect(view, QtCore.SIGNAL("modeChanged"), self.setStatusBarMode)
			QtCore.QObject.connect(view, QtCore.SIGNAL("periodChanged"), self.setStatusBarPeriod)
			#view.setupUi(tab)
			view.textBrowser.setSearchPaths(QStringList(cwd))
			self.views.append(view)
			self.tabWidget.setUpdatesEnabled(False)
			i = self.tabWidget.addTab(tab, QtGui.QIcon(cwd + "/pixmaps/garystar.png"), '')
			self.tabWidget.setTabText(i, tabText)
			#addTab future with fav.ico...
			self.tabWidget.setCurrentIndex(i)
			self.tabWidget.setUpdatesEnabled(True)
		else:
			if containedIndex != 0:
				if not self.getCurrentViewPerIndex(containedIndex).isLogined() and not self.getCurrentViewPerIndex(containedIndex).isReconnected():
					#self.getCurrentViewPerIndex(containedIndex).doLogin()
					self.tabWidget.setCurrentIndex(containedIndex)
				elif not self.getCurrentViewPerIndex(containedIndex).isLogined() and self.getCurrentViewPerIndex(containedIndex).isReconnected():
					self.getCurrentViewPerIndex(containedIndex).reconnect()
					self.tabWidget.setCurrentIndex(containedIndex)
				else:
					self.tabWidget.setCurrentIndex(containedIndex)
		#	view.doLogin()

	def currentViewConnect(self):
		if self.views[self.getCurrentViewIndex() -1].isReconnected() is False:
			self.views[self.getCurrentViewIndex() - 1].doLogin()

	def removeCurrentView(self):
			i = self.getCurrentViewIndex() - 1
			self.views[i].disconnect()
			self.tabWidget.removeTab(i + 1)
			self.tempviews.append(self.views.pop(i))
			#shits. tempviews willnot deleted. Memory & Performance usage?

	def getCurrentViewIndex(self):
		return self.tabWidget.currentIndex()

	def getCurrentView(self):
		return self.views[self.getCurrentViewIndex() - 1]

	def getCurrentViewPerIndex(self, i):
		return self.views[i - 1]

	def getViews(self):
		return self.views

	def currentViewReconnect(self):
		self.views[self.getCurrentViewIndex() - 1].reconnect()

	def currentDisconnect(self):
		self.views[self.getCurrentViewIndex() - 1].disconnect()

	def updateStar(self, view):
		if self.getCurrentViewIndex() == 0:
			self.tabWidget.setTabIcon(self.views.index(view) + 1, QtGui.QIcon(cwd + "/pixmaps/goldstar.png"))
		else:
			if view is not self.getCurrentView():
				if view in self.getViews():
					self.tabWidget.setTabIcon(self.views.index(view) + 1, QtGui.QIcon(cwd + "/pixmaps/goldstar.png"))

	def setCurrentTabGaryStar(self):
		self.tabWidget.setTabIcon(self.getCurrentViewIndex(), QtGui.QIcon(cwd + "/pixmaps/garystar.png"))

	def setStatusBarLastUpdate(self, view):
		if self.getCurrentViewIndex() != 0:
			if view is self.getCurrentView():
				self.ingcApp.statusBarLabelWhen.setText(u'最后更新@ ' + view.updateTime)

	def setStatusBarMode(self, view):
		if self.getCurrentViewIndex() != 0:
			if view is self.getCurrentView():
				if view.mode == 'me':
					mode = 'Me'
				elif view.mode == 'friends':
					mode = 'Friends'
				elif view.mode == 'everyone':
					mode = 'Everyone'
				self.ingcApp.statusBarLabelStatus.setText(u'模式: ' + mode)

	def setStatusBarPeriod(self, view):
		if self.getCurrentViewIndex() != 0:
			if view is self.getCurrentView():
				self.ingcApp.statusBarLabelPeriod.setText(u'周期: ' + str(view.period) + u's')
